package com.store.app.http;

import android.util.Log;

import com.store.app.MainActivity;
import com.store.app.utils.http.MD5;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.util.List;

public class Submit extends Thread {

	public static JSONObject submit(List<NameValuePair> params, String uri) {
		HttpPost httpRequest = new HttpPost(uri);
		JSONObject obj = null;
		try {
			// 发出HTTP request
			httpRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(params,
					HTTP.UTF_8));
			// 取得HTTP response
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequest);
			String strResult = EntityUtils.toString(httpResponse.getEntity());
			obj = new JSONObject(strResult);
		} catch (Exception e) {

		}
		return obj;
	}
	public static String genPackageSign(List<NameValuePair> params) {


		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < params.size(); i++) {
			sb.append(params.get(i).getName());
			sb.append('=');
			sb.append(params.get(i).getValue());
			sb.append('&');
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(MainActivity.security_code);
		Log.v("zyl","sb:"+sb.toString());
		String packageSign = MD5.getMessageDigest(sb.toString().getBytes());
		Log.e("orion", packageSign);
		return packageSign;
	}

	public static String genPackageSignUser(List<NameValuePair> params) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < params.size(); i++) {
			sb.append(params.get(i).getName());
			sb.append('=');
			sb.append(params.get(i).getValue());
			sb.append('&');
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(MainActivity.user_security_code);
		Log.v("zyl","sb.toString():"+sb.toString());

		String packageSign = MD5.getMessageDigest(sb.toString().getBytes());
		Log.v("zyl","sb.toString() packageSign:"+packageSign);
		Log.e("orion", packageSign);


		return packageSign;
	}
}